<?php
namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;

use App\Http\Requests;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\Exceptions\ValidatorException;
use App\Http\Requests\CartCreateRequest;
use App\Http\Requests\CartUpdateRequest;
use App\Services\Cart\CartService;

/**
 * Class CartsController.
 *
 * @package namespace App\Http\Controllers\Api\V1;
 */
class CartsController extends ApiController
{

    protected $cartService;

    public function __construct(CartService $cartService)
    {
        $this->cartService=$cartService;
    }

    /**
     * @role 增加数量
     * @author Jianhe
     * @return mixed
     */
    public function increase()
    {
        $goods_id=13337;
        $act_id=0;
        $giving=0;
        $num=2;
        $user_id=95;

        $this->cartService->increase($goods_id,$act_id,$giving,$num,$user_id);
        $res=$this->cartService->getMyCart($user_id);
        return response()->json([
            'error'   => true,
            'message' => $res
        ]);
    }

    /**
     * @role 减少数量
     * @author Jianhe
     * @return mixed
     */
    public function reduce(){
        $goods_id=13337;
        $act_id=0;
        $giving=0;
        $num=2;
        $user_id=95;

        $this->cartService->reduce($goods_id,$act_id,$giving,$num,$user_id);
        $res=$this->cartService->getMyCart($user_id);
        return response()->json([
            'error'   => true,
            'message' => $res
        ]);
    }

    /**
     * @role 模拟确认订单获取购物车数据
     * @author Jianhe
     * @return mixed
     */
    public function getGoodsForOrder(){
        $user_id=95;
        $bonus_id=0;

        $res=$this->cartService->getGoodsForOrder($user_id,$bonus_id);
        return response()->json([
            'error'   => true,
            'message' => $res
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  CartCreateRequest $request
     *
     * @return \Illuminate\Http\Response
     *
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function store(CartCreateRequest $request)
    {
        try {

            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);

            $cart = $this->repository->create($request->all());

            $response = [
                'message' => 'Cart created.',
                'data'    => $cart->toArray(),
            ];

            if ($request->wantsJson()) {

                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (ValidatorException $e) {
            if ($request->wantsJson()) {
                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessageBag()
                ]);
            }

            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $cart = $this->repository->find($id);

        if (request()->wantsJson()) {

            return response()->json([
                'data' => $cart,
            ]);
        }

        return view('carts.show', compact('cart'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cart = $this->repository->find($id);

        return view('carts.edit', compact('cart'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  CartUpdateRequest $request
     * @param  string            $id
     *
     * @return Response
     *
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function update(CartUpdateRequest $request, $id)
    {
        try {

            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);

            $cart = $this->repository->update($request->all(), $id);

            $response = [
                'message' => 'Cart updated.',
                'data'    => $cart->toArray(),
            ];

            if ($request->wantsJson()) {

                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (ValidatorException $e) {

            if ($request->wantsJson()) {

                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessageBag()
                ]);
            }

            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $deleted = $this->repository->delete($id);

        if (request()->wantsJson()) {

            return response()->json([
                'message' => 'Cart deleted.',
                'deleted' => $deleted,
            ]);
        }

        return redirect()->back()->with('message', 'Cart deleted.');
    }
}
